<?php
namespace Home\Controller;
use Think\Controller;
class ParameterValueController extends CommonController {
    public function data_list(){
        $limit = I("pageSize", 30, "intval");
        $page = I("pageCurrent", 1, "intval");
        $pa_id = I('pa_id');
        $condition['pav_pa_id'] = array('eq',$pa_id);
        $this->assign('pa_id',$pa_id);
        $parameterValue_model = D("ParameterValue");
        $count = $parameterValue_model->get_count($condition);
        $dataList = $parameterValue_model->get_parameterValue_list($condition, $limit, $page);
        $this->assign("count",$count);
        $this->assign("dataList",$dataList);
    	$this->display();
    }
    public function operation(){
        $ot = I("ot");
        $pav_id = $_GET['pav_id'];
        $pa_id = $_GET['pa_id'];
        $parameterValue_model = D("ParameterValue");
        if($ot==null||$ot==''){
            $ot = $_GET["ot"];
        }
        if($ot=='add'&&$pav_id==null){
            if(IS_POST){
                $data['pav_name'] = $_POST["pav_name"];
                $data['pav_value'] = $_POST["pav_value"];
                $data['pav_pa_id'] = $_POST["pav_pa_id"];
                $result = $parameterValue_model->add($data);
                if($result){
                    exit(success('成功！',true));
                }else{
                    exit(error('失败！'));
                }
            }else{
                $this->assign("ot",'add');
                $this->assign("pa_id",$pa_id);
                $this->display();
            }
            $this->display();
        }if($ot=='edit'){
            if(IS_POST&&$_POST['pav_id']!=null){
                $condition['pav_id'] = $_POST['pav_id'];
                $data['pav_name'] = $_POST["pav_name"];
                $data['pav_value'] = $_POST["pav_value"];
                
                /* 修改订单参数 */
                $pav_id = $_POST['pav_id'];
                $where['pav_id'] = $pav_id;
                $parameterValue = $parameterValue_model->where($where)->find();
                $pa_id = $parameterValue['pav_pa_id'];
                $orderList = M("Order")->where(array('or_status'=>array("neq",2)))->select();
                foreach ($orderList as $key => $value) {
                    $run = true;
                    $orderParams_json = $value['or_param'];
                    $orderParams = json_decode($orderParams_json,true);
                    foreach ($orderParams as $k => $v) {
                        if($run){
                            $parameter = M('Parameter')->where(array('pa_key'=>$k))->find();
                            if($parameter['pa_id'] == $pa_id&&$v==$parameterValue['pav_value']){
                                $orderParams[$k] = $_POST["pav_value"];
                                $run = false;
                            }
                        }
                    }
                    if(!$run){
                        M("Order")->where(array('or_id'=>$value['or_id']))->save(array('or_param'=>json_encode($orderParams)));
                    }
                }

                $result = $parameterValue_model->where($condition)->save($data);
                if($result){
                    exit(success('成功！',true));
                }else{
                    exit(error('失败！'));
                }
            }else{
                $condition['pav_id'] = $pav_id;
                $data = $parameterValue_model->where($condition)->find();
                $this->assign("ot",'edit');
                $this->assign("data",$data);
                $this->display();
            }
        }else if($pav_id!=null&&$ot=='del'){
            $condition['pav_id'] = $pav_id;
            $parameterValue = $parameterValue_model->where($condition)->find();
            $result = $parameterValue_model->where($condition)->delete();

            $pa_id = $parameterValue['pav_pa_id'];
            $parameterValueData = $parameterValue_model->where(array('pav_pa_id'=>$pa_id))->order('pav_value asc')->find();
            /* 更改未完成订单参数 */
            $orderList = M("Order")->where(array('or_status'=>array("neq",2)))->select();
            foreach ($orderList as $key => $value) {
                $run = true;
                $orderParams_json = $value['or_param'];
                $orderParams = json_decode($orderParams_json,true);
                foreach ($orderParams as $k => $v) {
                    if($run){
                        $parameter = M('Parameter')->where(array('pa_key'=>$k))->find();
                        if($parameter['pa_id'] == $pa_id&&$v==$parameterValue['pav_value']){
                            $orderParams[$k] = $parameterValueData['pav_value'];
                            $run = false;
                        }
                    }
                }
                if(!$run){
                    M("Order")->where(array('or_id'=>$value['or_id']))->save(array('or_param'=>json_encode($orderParams)));
                }
            }
            
            if($result){
                exit(success('成功！',false));
            }else{
                exit(error('失败！'));
            }
        }else{
            exit(error('参数错误！'));
        }
    }

    public function getParamsValue(){

    }
}